package com.wrl.mmall.util;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 流读取
 */
public class PropertiesUtil {

	private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
	
	private static Properties props;
	
	//写这个Util的实现，在tomcat启动的时候就要读取里面的配置，使用静态块
	static {
		String fileName = "mmall.properties";
		props = new Properties();
		try {
			props.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName),"UTF-8"));
		} catch (IOException e) {
			logger.error("配置文件读取异常", e);
		}
	}
	
	//通过mmall.properties里面的key获取value
	public static String getProperty(String key) {
		String value = props.getProperty(key.trim());
		if(StringUtils.isBlank(value)) {
			return null;
		}
		return value.trim();
	}
	
	public static String getProperty(String key, String defaultValue) {
		String value = props.getProperty(key.trim());
		if(StringUtils.isBlank(value)) {
			value = defaultValue;
		}
		return value.trim();
	}
	
}
